import {createRouter, createWebHistory} from "vue-router";
import {showFailToast} from "vant";
import {store} from "@/store";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "",
            name: "defaultHome",
            component: () => import("@/views/home/HomePage.vue"),
            meta: {
                title: "图书兄弟"
            }
        },
        {
            path: "/",
            name: "home",
            component: () => import("@/views/home/HomePage.vue"),
            meta: {
                title: "图书兄弟"
            }
        },
        {
            path: "/detail",
            name: "detail",
            component: () => import("@/views/detail/DetailPage.vue"),
            meta: {
                title: "图书兄弟-商品分类"
            }
        },
        {
            path: "/category",
            name: "category",
            component: () => import("@/views/category/CategoryPage.vue"),
            meta: {
                title: "图书兄弟-商品分类"
            }
        },
        {
            path: "/shopcart",
            name: "shopcart",
            component: () => import("@/views/shopcart/ShopCartPage.vue"),
            meta: {
                title: "图书兄弟-购物车",
                isAuthRequired: true, // 授权才能访问 标志
            }
        },
        {
            path: "/profile",
            name: "profile",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/profile/ProfilePage.vue"),
            meta: {
                title: "图书兄弟-个人中心",
                isAuthRequired: true, // 授权才能访问 标志
            },
        },
        {
            path: "/collect",
            name: "collect",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/profile/MyCollectionPage.vue"),
            meta: {
                title: "图书兄弟-新用户注册"
            }
        },
        {
            path: "/account",
            name: "account",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/profile/PasswordModifyPage.vue"),
            meta: {
                title: "图书兄弟-新用户注册"
            }
        },
        {
            path: "/register",
            name: "register",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/profile/RegisterPage.vue"),
            meta: {
                title: "图书兄弟-新用户注册"
            }
        },
        {
            path: "/login",
            name: "login",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/profile/LoginPage.vue"),
            meta: {
                title: "图书兄弟-用户登录"
            }
        },
        {
            path: "/create/order",
            name: "createOrder",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/order/CreateOrderPage.vue"),
            meta: {
                title: "图书兄弟-创建订单"
            }
        },
        {
            path: "/order",
            name: "order",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/order/OrderPage.vue"),
            meta: {
                title: "图书兄弟-订单页面"
            }
        },
        {
            path: "/order/detail",
            name: "orderDetail",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/order/OrderDetailPage.vue"),
            meta: {
                title: "图书兄弟-订单详情"
            }
        },
        {
            path: "/about",
            name: "about",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/profile/AboutWePage.vue"),
            meta: {
                title: "图书兄弟-关于我们"
            }
        },
        {
            path: "/address",
            name: "address",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/profile/AddressPage.vue"),
            meta: {
                title: "图书兄弟-地址管理"
            }
        },
        {
            path: "/address/edit",
            name: "addressEdit",
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import("@/views/profile/AddressEditPage.vue"),
            meta: {
                title: "图书兄弟-地址编辑"
            }
        },

    ]
});

router.beforeEach((to, from, next) => {

    if(to.meta.isAuthRequired && !store.state.user.isLogin){
        showFailToast("您还没有登录，请先登录！");
        next('/login');
    }else{
        next();
    }
    document.title = to.meta.title as string;
});
export default router;
